package com.fs.service.impl;


import com.fs.dao.ReplyDao;
import com.fs.dao.UserDao;
import com.fs.pojo.Reply;
import com.fs.pojo.User;
import com.fs.service.ReplyService;
import com.fs.utils.BeanFactories;
import com.fs.vo.ReplyVO;
import net.sf.cglib.beans.BeanCopier;

import java.util.List;
import java.util.stream.Collectors;

/**
 * (TbReply)表服务实现类
 *
 * @author makejava
 * @since 2022-11-15 10:21:17
 */

public class ReplyServiceImpl implements ReplyService {

    private ReplyDao replyDao = BeanFactories.getBean(ReplyDao.class);
    private UserDao userDao = BeanFactories.getBean(UserDao.class);

    @Override
    public List<ReplyVO> getAllRepliesByDiscusstionId(String id) {

        List<Reply> replies = replyDao.queryAllByDiscusstionId(id);

        if(replies == null)
            return null;

        List<ReplyVO> vos = replies.stream().map(reply -> {

            ReplyVO vo = new ReplyVO();

            User user = userDao.queryById(reply.getUserId());

            BeanCopier.create(User.class, ReplyVO.class, false).copy(user, vo, null);
            BeanCopier.create(Reply.class, ReplyVO.class, false).copy(reply, vo, null);

            return vo;
        }).collect(Collectors.toList());

        return vos;
    }
}
